home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 199_01 / ged8.c < prev    next >
Text File  |  1987-12-15  |  6KB  |  308 lines

  1. /*
  2. Header:          CUG199;
  3. Title:           Module 8 of ged editor;
  4. Last Updated:    12/01/87;
  5.  
  6. Description:    "PURPOSE: get and put text lines into and out of storage";
  7.  
  8. Keywords:        e, editor, qed, ged, DeSmet, MSDOS;
  9. Filename:        ged8.c;
  10. Warnings:       "O file must be present during link for ged";
  11.  
  12. Authors:         G. Nigel Gilbert, James W. Haefner, and Mel Tearle;
  13. Compiler:        DeSmet 2.51;    s
  14.  
  15.  
  16. References:
  17. Endref;
  18. */
  19.  
  20. /*
  21. e/qed/ged  screen editor
  22.  
  23. (C) G. Nigel Gilbert, MICROLOGY, 1981 - August-December 1981
  24.  
  25. Modified:  Aug-Dec   1984:  BDS-C 'e'(vers 4.6a) to 'qe' (J.W. Haefner)
  26.            March     1985:  BDS-C 'qe' to DeSmet-C 'qed' (J.W. Haefner)
  27.  
  28.            May       1986:  qed converted to ged         (Mel Tearle)
  29.            August    1987:  ged converted to MSC 4.0     (Mel Tearle)
  30.  
  31. File:      ged8.c
  32.  
  33. Functions: loc, gettext, getline, writ_txb, deltp, puttext,
  34.            readtext, opentext, balloc, error, addhistory
  35. */
  36.  
  37.  
  38. #ifndef  TC
  39. #include "ged.h"
  40. #else
  41. #include "ged.t"
  42. #endif
  43.  
  44.  
  45. /* returns line + move, adjusted to be within text
  46.  */
  47. int loc(line,move)
  48. int line, move;
  49. {
  50. int y;
  51.  
  52. if ( ( y = line + move ) < 1 )  y = 1;
  53. if ( lastl == UNKNOWN  &&  y > lastread )  readtext( y );
  54. if ( y > lastl )  return lastl;
  55.  
  56. return y;
  57. }
  58.  
  59.  
  60. /* makes 'line' the current line
  61.  */
  62. void gettext(line)
  63. int line;
  64. {
  65. char *getline();
  66.  
  67. strcpy( text, getline( line ) );
  68. cline = line;
  69. }
  70.  
  71.  
  72. /* returns address of text of 'line'
  73.  * and updates page usage
  74.  */
  75. char *getline(line)
  76. int line;
  77. {
  78. int  slot;
  79.  
  80. line = loc( line, 0 );
  81.  
  82. if ( ( slot = pageloc[ tp[line].page] ) <= 0 )
  83.        slot = swappin( tp[line].page );
  84.  
  85. if ( usage[slot] > FREE )
  86.      usage[slot] = ++pclock;
  87.  
  88. return  slotaddr[slot] + tp[line].moffset;
  89. }
  90.  
  91.  
  92. /* inserts 'txt' after 'line'
  93.  */
  94. int writ_txb( line, txt )
  95. int  line;
  96. char *txt;
  97. {
  98. int  l, p, balloc();
  99. struct  addr  anaddr;
  100.  
  101. addhistory( line, HISTINSERT );
  102.  
  103. if ( ( char * ) &tp[lastread+2] >= slotaddr[tppages] )  {
  104.   /* need another slot to store tp's in
  105.    */
  106.   if ( ( tppages + 2  ) == slotsinmem )  {
  107.          error( " Too many lines of text " );
  108.          return  FAIL;
  109.   }
  110.   if ( usage[tppages] == NOTAVAIL )  /* ie page is being alloc'ed into */
  111.        allocp = PAGESIZE+1;          /* force alloc into new page */
  112.  
  113.   if ( usage[tppages] != FREE )
  114.        swapout(tppages);             /* changed */
  115.  
  116.   usage[tppages++] = NOTAVAIL;
  117. }
  118.  
  119. #ifndef  DS
  120.   if ( ++line < ++lastread )
  121.        memcpy( &tp[line+1], &tp[line], (lastread-line)*sizeof(anaddr) );
  122. #else
  123.   if ( ++line < ++lastread )
  124.        _move( (lastread-line)*sizeof(anaddr), &tp[line], &tp[line+1] );
  125. #endif
  126.  
  127. tp[line].moffset = p = balloc( 1 + strlen( txt ) );
  128. tp[line].page = newpage;
  129. strcpy( npaddr+p, txt );
  130.  
  131. if ( lastl != UNKNOWN )  lastl++;
  132.  
  133. return  line;
  134. }
  135.  
  136.  
  137. /* delete line by shifting pointers
  138.  */
  139. void deltp(from)
  140. int from;
  141. {
  142. struct  addr  anaddr;
  143.  
  144. addhistory( from, HISTDELETE );    /* add for undo */
  145.  
  146. #ifdef  MSC
  147.    memmove( &tp[from], &tp[from+1], ( lastread - from )*sizeof(anaddr) );
  148. #else
  149.   _move( ( lastread - from )*sizeof( anaddr ), &tp[from+1], &tp[from] );
  150. #endif
  151.  
  152. if ( lastl != UNKNOWN )  lastl--;
  153. lastread--;
  154. if ( lastl < 1 )  lastl = 1;
  155. if ( lastread < 1 )  lastread = 1;
  156. }
  157.  
  158.  
  159. /* replaces cline's text if it has been altered
  160.  */
  161. void puttext()
  162. {
  163. int p, cp, balloc();
  164.  
  165. if ( altered )  {
  166.      addhistory( cline, HISTREPLACE );   /* add for undo */
  167.      altered = NO;
  168.      if ( !trail )  {
  169.           for ( cp = strlen( text )-1;
  170.                 cp >= 0  &&  isspace( text[cp] ); cp-- )
  171.                 text[cp] = '\0';
  172.      }
  173.      tp[cline].moffset = p = balloc( 1 + strlen( text ) );
  174.      tp[cline].page = newpage;
  175.      strcpy( npaddr+p, text );
  176.    }
  177. }
  178.  
  179.  
  180. /* Reads file being edited into virtual memory
  181.  * until 'line' is read, or eof.
  182.  * If eof, sets lastl to number of last line read.
  183.  * File is assumed to be already open
  184.  */
  185. void readtext(line)
  186. int line;
  187. {
  188. char txt[LLIM], *t;
  189. int  i, c, l;
  190.  
  191. storehist = NO;
  192.  
  193. while ( lastread < line )  {
  194.   for ( i = 0, t = &txt[0]; i < LLIM  &&
  195.         ( ( c = egetc( ( struct iobuffer *)textbuf) ) >= ' ' ||
  196.         c != '\n' && c != DFAIL  &&  c != ENDFILE ); i++ )  {
  197.         if ( c )  {
  198.             *t = ( char ) c;
  199.              t++;
  200.         }
  201.         else i--;
  202.         }
  203.   if ( txt[i-1] == '\r' )
  204.        t--;
  205.   *t = '\0';
  206.  
  207.   if ( ( l = writ_txb( lastread, txt ) ) == FAIL )
  208.          goto  toomuch;
  209.   else
  210.     if ( ( lastread = l )%100 == 0 )
  211.            putlineno( lastread );
  212.  
  213.     if ( ( goteof = ( char ) ( c == ENDFILE ) ) || c == DFAIL )  goto eof;
  214. }
  215. goto ok;
  216.  
  217. eof:
  218.   ffclose( ( struct iobuffer *) textbuf );
  219. toomuch:
  220.   lastl = lastread;
  221. ok:
  222.   storehist = YES;
  223. }
  224.  
  225.  
  226. /* open the file being edited for reading
  227.  */
  228. int opentext(name)
  229. char *name;
  230. {
  231. if ( ffopen( name, ( struct iobuffer * ) textbuf ) == FAIL )  {
  232.  
  233. /* attempt to open with default extension added
  234.  */
  235.   strcat( name, defext );
  236.  
  237.   if ( ffopen( name, ( struct iobuffer * ) textbuf ) == FAIL )  {
  238.        error( " Can't open file " );
  239.        name[0] = '\0';
  240.        lastl = 1;
  241.        return  FAIL;
  242.   }
  243. }
  244. return  YES;
  245. }
  246.  
  247.  
  248. /* allocate and return the offset in newpage of a vector size n
  249.  */
  250. int balloc(n)
  251. unsigned n;
  252. {
  253. int slot;
  254.  
  255. if ( ( allocp + n ) >= PAGESIZE )  {
  256.  
  257. /* no room in current newpage; get another
  258.  */
  259. if ( pageloc[newpage] > 0 )  usage[pageloc[newpage]] = INUSE;
  260.      pageloc[++newpage] = slot = freememslot();
  261.      usage[slot] = NOTAVAIL;
  262.      allocp = 0;
  263.      npaddr = slotaddr[slot];
  264. }
  265. allocp += n;
  266. return  allocp-n;
  267. }
  268.  
  269.  
  270. void error(message)
  271. char *message;
  272. {
  273. if ( errmess != NULL )  return;
  274.  
  275. scr_delete( 0, 0 );
  276. scr_cursoff();
  277. scr_aputs( 0, 0, message, REVS );  scr_ci();
  278. blankedmess = YES;
  279. scr_curson();
  280. resetcursor();
  281. inbufp = 0;
  282. }
  283.  
  284.  
  285. void addhistory(l,type)
  286. int l;
  287. int type;
  288. {
  289. struct  histstep  *step;
  290.  
  291. if ( !storehist )  return;
  292.  
  293. step = &history[histptr];
  294.  
  295. step->histp.page = tp[l].page;
  296. step->histp.moffset = tp[l].moffset;
  297.  
  298. step->histline = l;
  299. step->histtype = type;
  300. step->histcomm = ncommand;
  301.  
  302. if ( ++histptr == HISTLEN )  histptr = 0;
  303. if ( histcnt < HISTLEN )  histcnt++;
  304. }
  305.  
  306.  
  307. /* that's all */
  308.